package
{
	import flash.utils.setTimeout;
	import flash.utils.clearTimeout;
	import flash.display.Sprite;
	import flash.text.TextField;

	public class Toast extends Sprite
	{
		private static var _main:Toast;
		private static function get stageW():int
		{
			if(main.stage)
				return main.stage.stageWidth;
			return 1024;
		}
		private static function get stageH():int
		{
			if(main.stage)
				return main.stage.stageHeight;
			return 768;
		}
		public static function get main():Toast
		{
			if(_main==null)
				_main = new Toast();
			return _main;
		}
		private static var txt:TextField;
		private static var bg:Sprite;
		public function Toast()
		{
			_main = this;
			if(txt==null)
				txt = ViewSet.makeTxtLine("",30,0xffffff);
			if(bg==null)
				bg = new Sprite();
			bg.addChild(txt);
			visible = false;
			addChild(bg);
		}

		private static var timeoutid:uint;
		public static var msg:String="";
		public static function show(s:String,time:int=1500):void
		{
			msg = s;
			TweenLite.killTweensOf(bg);
			main.visible = true;
			txt.text = s;
			txt.x = -txt.width/2;
			main.x = stageW/2;
			main.y = stageH*.75;
			CONFIG::debugging
			{
				main.y = stageH*.5;
			}
			bg.alpha = 1.0;
			with(bg)
			{
				graphics.clear();
				graphics.beginFill(0x333333);
				graphics.drawRoundRect(txt.x-txt.height/2,txt.y-txt.height/2, txt.width+txt.height, txt.height*2,txt.height,txt.height);
				graphics.endFill();
			}
			TweenLite.from(bg,.3,{y:stageH});
			clearTimeout(timeoutid);
			timeoutid = setTimeout(fadeout, time);
		}

		private static function fadeout():void
		{
			clearTimeout(timeoutid);
			TweenLite.killTweensOf(bg);
			TweenLite.to(bg,.3,{alpha:0,onComplete:hide});
		}

		public static function hide():void
		{
			TweenLite.killTweensOf(bg);
			clearTimeout(timeoutid);
			main.visible = false;
		}
	}
}
